package com.shujia.flink.core

// 一定要导入这个包
import java.util.concurrent.CompletableFuture

import org.apache.flink.streaming.api.scala._


object StrramWordCount {
  def main(args: Array[String]): Unit = {

    //获取flink环境
    val env = StreamExecutionEnvironment.getExecutionEnvironment

    //设置并行度
    env.getConfig.setParallelism(2)

    //连接socket创建DataStream
    //nc -lk 8888
    val ds: DataStream[String] = env.socketTextStream("node1", 8888)

    val wordsDS = ds.flatMap(_.split(","))

    val kvDS = wordsDS.map(word => (word, 1))


    //keyBy  将相同的key分到同一个节点
    //    val keyDDS = kvDS.keyBy(_._1)
    //也可以指定下标
    val keyDDS = kvDS.keyBy(0)
    //组内求和
    val countDS = keyDDS.sum(1)

    //打印结果
    countDS.print()

    //启动flink程序
    env.execute()
  }
}
